<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类图片uid" prop="fileUid">
      <el-input v-model="dataForm.fileUid" placeholder="分类图片uid"></el-input>
    </el-form-item>
    <el-form-item label="分类名" prop="sortName">
      <el-input v-model="dataForm.sortName" placeholder="分类名"></el-input>
    </el-form-item>
    <el-form-item label="分类介绍" prop="content">
      <el-input v-model="dataForm.content" placeholder="分类介绍"></el-input>
    </el-form-item>
    <el-form-item label="点击数" prop="clickCount">
      <el-input v-model="dataForm.clickCount" placeholder="点击数"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="父UID" prop="parentUid">
      <el-input v-model="dataForm.parentUid" placeholder="父UID"></el-input>
    </el-form-item>
    <el-form-item label="排序字段" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          fileUid: '',
          sortName: '',
          content: '',
          clickCount: '',
          status: '',
          createTime: '',
          updateTime: '',
          parentUid: '',
          sort: '',
          isDelete: ''
        },
        dataRule: {
          fileUid: [
            { required: true, message: '分类图片uid不能为空', trigger: 'blur' }
          ],
          sortName: [
            { required: true, message: '分类名不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '分类介绍不能为空', trigger: 'blur' }
          ],
          clickCount: [
            { required: true, message: '点击数不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          parentUid: [
            { required: true, message: '父UID不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/resourcesort/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.fileUid = data.resourceSort.fileUid
                this.dataForm.sortName = data.resourceSort.sortName
                this.dataForm.content = data.resourceSort.content
                this.dataForm.clickCount = data.resourceSort.clickCount
                this.dataForm.status = data.resourceSort.status
                this.dataForm.createTime = data.resourceSort.createTime
                this.dataForm.updateTime = data.resourceSort.updateTime
                this.dataForm.parentUid = data.resourceSort.parentUid
                this.dataForm.sort = data.resourceSort.sort
                this.dataForm.isDelete = data.resourceSort.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/resourcesort/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'fileUid': this.dataForm.fileUid,
                'sortName': this.dataForm.sortName,
                'content': this.dataForm.content,
                'clickCount': this.dataForm.clickCount,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'parentUid': this.dataForm.parentUid,
                'sort': this.dataForm.sort,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
